package com.macro.mall.portal.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.mapper.CmsMemberReportMapper;
import com.macro.mall.model.CmsMemberReport;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.CmsMemberReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * CmsMemberReport的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class CmsMemberReportServiceImpl implements CmsMemberReportService {

    private boolean isDebugEnabled;

	@Autowired
	private CmsMemberReportMapper cmsMemberReportMapper;
	// TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

	@Override
	public List<CmsMemberReport> find(CmsMemberReport value) {
		//TODO这里可以做通过Assist做添加查询
        List<CmsMemberReport> result = null;
        try {
            result = cmsMemberReportMapper.selectCmsMemberReport(null);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CmsMemberReport数据集-->结果:", result);
        }
		return result;
	}

	@Override
	public CommonResult saveNotNull(CmsMemberReport value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将CmsMemberReport中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
		int result = 0;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            Long id = memberDetails.getUmsMember().getId();
            String username = memberDetails.getUmsMember().getUsername();
            value.setReportMemberName(id+"");
            value.setMobile(username);
            result = cmsMemberReportMapper.insertNotNullCmsMemberReport(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行将CmsMemberReport中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}


}